package com.nnnu.wsnackshop.controller.user;

import com.nnnu.wsnackshop.constant.MessageConstants;
import com.nnnu.wsnackshop.pojo.vo.BannerVO;
import com.nnnu.wsnackshop.result.Result;
import com.nnnu.wsnackshop.service.IBannersService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 轮播图Controller（用户端）
 */
@RestController("userBannersController")
@RequestMapping("/api/user/banners")
@Tag(name = "用户端-轮播图", description = "用户端轮播图接口")
@Slf4j
@RequiredArgsConstructor
public class BannersController {

    private final IBannersService bannersService;

    /**
     * 获取启用的轮播图列表
     */
    @Operation(summary = "查询启用的轮播图")
    @GetMapping
    @Cacheable(cacheNames = "userBanners", key = "'banners'", unless = "#result.data == null")
    public Result<List<BannerVO>> getBanners() {
        log.info("[用户端] 查询轮播图");
        List<BannerVO> list = bannersService.getUserBanners();
        return Result.success(list, MessageConstants.SELECT_OK);
    }

    /**
     * 根据ID获取轮播图详情
     */
    @GetMapping("/{id}")
    @Operation(description = "根据ID获取轮播图详情")
    public Result<BannerVO> getBannerById(@PathVariable Integer id) {
        BannerVO banner = bannersService.getBannerById(id);
        if (banner == null) {
            return Result.error(404, "轮播图不存在");
        }
        return Result.success(banner);
    }
}